/**
 * 显示消息提示框
 * @param content 提示的标题
 */
function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content,
		// mask: true //是否有透明蒙层，默认为false 
		//图片优先级更高但您应该使用本地的而非线上的图片链接
		// image: '../../../static/logo.png',
		duration: 2000//持续时间为 2秒
	})
}
/**
 * 有上拉列表的提示框
 * @param list 提示的列表数据
 */
function showActionSheet(itemList) {
	uni.showActionSheet({
		itemList: itemList,//只有四个元素才生效
		success(res) {
			console.log(res.tapIndex)
		},
		fail(res) {
			console.log(res.errMsg)
		}
	})
}

/**
 * 显示程工消息提示框
 * @param content 提示的标题
 */
function succesToast(content) {
	uni.showToast({
		duration: 2000,
		title: content
	})
}
/**
 * 显示加载提示框
 * @param content 提示的标题
 */
function  showLoading (str)  {
    uni.showLoading({
			title: str?str:'加载中...',
				duration: 2000,
		})
}
// 隐藏loading
function hideLoading (){
	uni.hideLoading()
}
/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
function showConfirm(obj) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: obj.content,
			cancelText: obj.cancelText ? obj.cancelText : '取消', // 取消按钮的文字  
			confirmText: obj.confirmText ? obj.confirmText : '确定', // 确认按钮文字  
			confirmColor: obj.confirmColor ? obj.confirmColor : '', //删除字体的颜色
			cancelColor: obj.cancelColor ? obj.cancelColor : '', //取消字体的颜色
			success: function(res) {
				resolve(res)
			}
		})
	})
}
// 格式化成年月日
// 使用方式
// var year = getDateTime('year');  
// console.log(year);  // 2021
// var month = getDateTime('month');  
// console.log(month); // 2021-12
// var date = getDateTime('');
// console.log(date);  // 2021-12-03 09:00:00
// 日期格式化年月日函数
function formatDate(type, format) {
	var date = new Date();
	var hengGang = "-";
	var maoHao = ":";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var curDate = date.getDate();
	var curHours = date.getHours();
	var curMinutes = date.getMinutes();
	var curSeconds = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (curDate >= 0 && curDate <= 9) {
		curDate = "0" + curDate;
	}
	if (curHours >= 0 && curHours <= 9) {
		curHours = "0" + curHours;
	}
	if (curMinutes >= 0 && curMinutes <= 9) {
		curMinutes = "0" + curMinutes;
	}
	if (curSeconds >= 0 && curSeconds <= 9) {
		curSeconds = "0" + curSeconds;
	}
	var currentdate = "";
	if (type == 'format') {
		// 传递需要的日期 格式化成年月日 只要年月日  format是传递的日期参数
		currentdate = format.slice(0, 10)
		return currentdate;
	} else if (type == "year") {
		// 获取年份
		currentdate = year;
		return currentdate;
	} else if (type == "month") {
		// 获取年份月份
		currentdate = year + hengGang + month
		return currentdate
	} else if (type == "day") {
		// 获取年月日
		currentdate = year + hengGang + month + hengGang + curDate
		return currentdate
	} else if (type == "minutes") {
		// 获取年月日 时分
		currentdate =
			year + hengGang + month + hengGang + curDate + ' ' + curHours + maoHao + curMinutes
	} else {
		// 获取年月日 + 当前时间
		currentdate =
			year + hengGang + month + hengGang + curDate + ' ' + curHours + maoHao + curMinutes + maoHao + curSeconds
		return currentdate
	}
}

function friendlyDate(timestamp) {
	var formats = {
		'year': '%n% 年前',
		'month': '%n% 月前',
		'day': '%n% 天前',
		'hour': '%n% 小时前',
		'minute': '%n% 分钟前',
		'second': '%n% 秒前',
	};

	var now = Date.now();
	var seconds = Math.floor((now - timestamp) / 1000);
	var minutes = Math.floor(seconds / 60);
	var hours = Math.floor(minutes / 60);
	var days = Math.floor(hours / 24);
	var months = Math.floor(days / 30);
	var years = Math.floor(months / 12);

	var diffType = '';
	var diffValue = 0;
	if (years > 0) {
		diffType = 'year';
		diffValue = years;
	} else {
		if (months > 0) {
			diffType = 'month';
			diffValue = months;
		} else {
			if (days > 0) {
				diffType = 'day';
				diffValue = days;
			} else {
				if (hours > 0) {
					diffType = 'hour';
					diffValue = hours;
				} else {
					if (minutes > 0) {
						diffType = 'minute';
						diffValue = minutes;
					} else {
						diffType = 'second';
						diffValue = seconds === 0 ? (seconds = 1) : seconds;
					}
				}
			}
		}
	}
	return formats[diffType].replace('%n%', diffValue);
}
export default {
	friendlyDate,
	toast,
	succesToast,
	showConfirm,
	formatDate,
	hideLoading,
	showLoading,
	showActionSheet
}